package com.paic.icore.test.demo.util;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

/**
 * 时间工具类
 * 
 * @author chenxiaojian
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    
    private static String[] parsePatterns = {
    		"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM-dd HH:mm", "yyyy-MM", 
    		"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm", "yyyy/MM",
    		"yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd", "yyyy.MM.dd HH:mm", "yyyy.MM","yyyyMMddHHmmssZ"};

    /**
     * 获取当前Date型日期
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * @return String
     */
    public static String getNowDateStr() {
        return getNowByFormat(YYYY_MM_DD);
    }

    public static final String getNowTimeStr() {
        return getNowByFormat(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 
     * @param format
     * @return
     */
    public static final String getNowByFormat(final String format) {
        return parseDate(format, new Date());
    }

    public static final String parseDate(final Date date) {
        return parseDate(YYYY_MM_DD, date);
    }
    
    public static final String parseDateTime(final Date date) {
        return parseDate(YYYY_MM_DD_HH_MM_SS, date);
    }
    
    public static final Long parseDateTime(final String date) {
        Date d = parseDate(YYYY_MM_DD_HH_MM_SS, date);
        return d == null ? null : d.getTime();
    }

    public static final String parseDate(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }
    
    public static final Date parseDate(final String format, final String str) {
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (Exception e) {
             e.printStackTrace();
             return null;
        }
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDiff(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
    
    /**
     * 获取两个日期差, 默认返回毫秒时间戳
     * @param start
     * @param end
     * @param divisor 1毫秒  1000秒
     * @return
     */
    public static long getDiff(String start, String end, long divisor) {
        Date sDate = DateUtils.parseDate(start);
        Date eDate = DateUtils.parseDate(end);
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(sDate);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(eDate);
        return (eDate.getTime() - sDate.getTime()) / (divisor == 0 ? 1 : divisor);
    }
    
    
    /**
     * 获取间隔日期
     * @param date
     * @param diff > 0 往后, < 0 往前
     * @return
     */
    public static String getDiff(String date, int diff) {
        Date d = parseDate(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + diff);
        return parseDate(c.getTime());
    }
    
    
    /**
     * 获取一天的开始时间 00:00:00
     * @param date
     * @return
     */
    public static String getDateStartTime(String date) {
    	Date d = parseDate(date);
    	if (d == null) {
			if (date.length() >= 10) {
				return date.substring(0, 10) + " 00:00:00";
			}
			return null;
		}
    	return parseDate(d) + " 00:00:00";
    }
    
    /**
     * 获取一天的结束时间 23:59:59
     * @param date
     * @return
     */
    public static String getDateEndTime(String date) {
    	Date d = parseDate(date);
    	if (d == null) {
			if (date.length() >= 10) {
				return date.substring(0, 10) + " 23:59:59";
			}
			return null;
		}
    	return parseDate(d) + " 23:59:59";
    }
    
    public static LinkedList<String> getRange(String start, String end) {
        LinkedList<String> ret = new LinkedList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(parseDate(YYYY_MM_DD, start));
        
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(parseDate(YYYY_MM_DD, end));
        
        while (!tempEnd.before(tempStart)) {
            ret.add(parseDate(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return ret;
    }
    
    public static Integer getYear(String date) {
        Date d = parseDate(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.YEAR);
    }
    
    public static Integer getMonth(String date) {
        Date d = parseDate(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.MONTH) + 1;
    }
    
    public static Integer getDay(String date) {
        Date d = parseDate(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.DAY_OF_MONTH);
    }
    
    public static Integer getHour(String date) {
        Date d = parseDate(date);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.HOUR);
    }
    
    public static void main(String[] args) {
        String s1 = "2019-03-04 02:12:33";
        String s2 = "2019-01-08";
        System.out.println(getYear(s1));
        System.out.println(getMonth(s1));
        System.out.println(getDay(s1));
        System.out.println(getHour(s1));
        System.out.println(getDiff(s2, 1));
    }
    
}
